package restful.api;


import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import restful.database.EM;
import restful.entity.Cartoon;
import restful.entity.Category;
import restful.bean.Result;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

@Path("/cartoon")
public class CartoonAPI {
    //访问地址 http://127.0.0.1:8080/restful/api/cartoon/alist
    //获取全部动画分类
    @POST
    @Path("/alist")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result cartoonCategoryList() {
        List<Category> categoryList = EM.getEntityManager()
                .createNamedQuery("Category.findAll",Category.class)
                .getResultList();

        return new Result(1,"获取列表成功",categoryList,"");
    }
    //添加动画分类
    @POST
    @Path("/addCategory")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result addCategory(Category category) {
        category.setId(0);
        category = EM.getEntityManager().merge(category);
        EM.getEntityManager().persist(category);
        EM.getEntityManager().getTransaction().commit();
        return new Result(1,"添加成功",category,"");
    }
    //更新分类信息
    @POST
    @Path("/updateCategory")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result updateCategory(Category category) {
        category = EM.getEntityManager().merge(category);
        EM.getEntityManager().persist(category);
        EM.getEntityManager().getTransaction().commit();
        return new Result(1, "修改成功", category, "");
    }
    //删除分类信息
    @POST
    @Path("/deleteCategory")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result deleteCategory(Category category) {
        EM.getEntityManager().remove(EM.getEntityManager().merge(category));
        EM.getEntityManager().getTransaction().commit();
        return new Result(1, "成功删除", category, "");
    }
    //获取全部动画
    @POST
    @Path("/blist")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result cartoonList() {
        List<Cartoon> cartoonList = EM.getEntityManager()
                .createNamedQuery("Cartoon.findAll",Cartoon.class)
                .getResultList();

        return new Result(1,"获取成功",cartoonList,"");
    }

    //根据分类查询动画列表
    @POST
    @Path("/clist")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result cartoonByCatgoryIdList(Integer catgoryId) {
        List<Cartoon> cartoonList = EM.getEntityManager()
                .createNamedQuery("Cartoon.findByCategoryId",Cartoon.class).setParameter("id",catgoryId)
                .getResultList();

        return new Result(1,"获取列表成功",cartoonList,"");
    }

    //添加动画
    @POST
    @Path("/addCartoon")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result addCartoon(Cartoon cartoon) {
        cartoon.setId(0);
        cartoon = EM.getEntityManager().merge(cartoon);
        EM.getEntityManager().persist(cartoon);
        EM.getEntityManager().getTransaction().commit();
        return new Result(1,"添加成功",cartoon,"");
    }
    //编辑动画
    @POST
    @Path("/updateCartoon")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result updateCartoon(Cartoon cartoon) {
        cartoon = EM.getEntityManager().merge(cartoon);
        EM.getEntityManager().persist(cartoon);
        EM.getEntityManager().getTransaction().commit();
        return new Result(1, "修改成功", cartoon, "");
    }
    //删除动画
    @POST
    @Path("/deleteCartoon")
    @Consumes("application/json;charset=UTF-8")
    @Produces("application/json;charset=UTF-8")
    public Result deleteCategory(Cartoon cartoon) {
        EM.getEntityManager().remove(EM.getEntityManager().merge(cartoon));
        EM.getEntityManager().getTransaction().commit();
        return new Result(1, "成功删除", cartoon, "");
    }


    // 从上传的 InputPart 中获取文件名
    private String getFileName(InputPart inputPart) {
        try {
            // 获取 Content-Disposition 头部中的文件名
            String contentDisposition = inputPart.getHeaders().getFirst("Content-Disposition");
            String[] items = contentDisposition.split(";");
            for (String item : items) {
                if (item.trim().startsWith("filename")) {
                    return item.substring(item.indexOf("=") + 2, item.length() - 1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "unknown";
    }

    //上传文件
    @POST
    @Path("/file")
    @Consumes("multipart/form-data")
    @Produces("application/json;charset=UTF-8")
    public Result fileUp(MultipartFormDataInput data) {
        try{
            // 获取上传的文件部分
            Map<String, List<InputPart>> formData = data.getFormDataMap();
            List<InputPart> fileParts = formData.get("data");  // "data" 是前端表单中文件字段的名称

            if (fileParts == null || fileParts.isEmpty()) {
                return new Result(1,"没有文件上传","666"," ");
            }

            // 获取上传的第一个文件的文件名
            String fileName = getFileName(fileParts.get(0));

            // 返回文件名的响应
            return new Result(1, "上传成功", fileName, "");
        } catch (Exception e) {

        }
        return new Result(1,"上传成功","你好","");
    }

}
